package com.kkb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.common.utils.StringUtils;
import com.kkb.page.PageDomain;
import com.kkb.page.TableDataInfo;
import com.kkb.page.TableSupport;
import com.kkb.pojo.TbSubOrder;
import com.kkb.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 订单列表controller
 **/
@Controller
@RequestMapping("orderController")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping()
    public String orderList() {
        return "order/orderList";
    }

    /**
     * 获取订单列表
     * @param order
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbSubOrder order) {
        startPage();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        List<TbSubOrder> orderLists = orderService.selectOrderList(order);
        rspData.setRows(orderLists);
        rspData.setTotal(new PageInfo(orderLists).getTotal());
        return rspData;
    }

    @GetMapping("/details/{ordersubId}")
    public String orderDetails(@PathVariable("ordersubId") String ordersubId, ModelMap mmap) {
        mmap.put("order", orderService.selectOrderById(ordersubId));
        return "order/orderDetails";
    }

    /**
     * 设置请求分页数据
     */
    private void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            PageHelper.startPage(pageNum, pageSize);
        }
    }
}
